package web.servlet.old;

import com.alibaba.fastjson.JSON;
import com.pzhu.pojo.Brand;
import com.pzhu.service.BrandService;
import com.pzhu.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
//@WebServlet("/addBrand")
public class AddBrandServlet extends HttpServlet {
    BrandService brandService = new BrandServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader reader = req.getReader();
        //1拿 到了post请求所传输的brand数据，json字符串
        String s = reader.readLine();
        //2将json字符串转换为对象
        Brand brand = JSON.parseObject(s, Brand.class);
        //3调用service中的add()添加数据到数据库表中
        brandService.add(brand);
        //通过resp带回成功标识
        resp.getWriter().write("success");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
